package com.zyd.controller.setmeal;

import com.zyd.constants.ErrorDef;
import com.zyd.exception.BusinessException;
import com.zyd.io.model.SafeEntity;
import com.zyd.io.page.PageBean;
import com.zyd.io.page.PageRequestBean;
import com.zyd.io.vo.res.ResBody;
import com.zyd.service.setmeal.SetMealService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/setmeals")
@ResponseBody
public class SetMealController {

    private static final Logger LOGGER = LoggerFactory.getLogger(SetMealController.class);

    @Autowired
    private SetMealService service;

    @PostMapping(value = "/mealList", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody queryStation(@RequestBody PageRequestBean request) {
        try {
            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "查询套餐成功");
            PageBean<SafeEntity> ret = service.querySetMealPage(request);
            resBody.setData(ret);
            return resBody;
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            LOGGER.error("", e);
            return new ResBody(ErrorDef.SYSTEM_ERROR, "查询套餐失败");
        }
    }


    @PostMapping(value = "/addMeal", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody addMeal(@RequestBody SafeEntity request) {
        try {
            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "添加套餐成功");
            service.addMeal(request);
            return resBody;
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            LOGGER.error("", e);
            return new ResBody(ErrorDef.SYSTEM_ERROR, "添加套餐失败");
        }
    }

    @PostMapping(value = "/findMeal", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody findMeal(@RequestBody Map<String, Object> request) {
        try {
            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "查询套餐成功");
            SafeEntity ret = service.findMeal((String) request.get("id"));
            resBody.setData(ret);
            return resBody;
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            return new ResBody(ErrorDef.SYSTEM_ERROR, "查询套餐失败");
        }
    }

    @PostMapping(value = "/updateMeal", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody updateMeal(@RequestBody SafeEntity request) {
        try {
            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "更新套餐成功");
            service.updateMeal(request);
            return resBody;
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            LOGGER.error("", e);
            return new ResBody(ErrorDef.SYSTEM_ERROR, "更新套餐失败");
        }
    }

    @PostMapping(value = "/deleteMeal", consumes = "application/json;charset=UTF-8", produces = "application/json;charset=UTF-8")
    public ResBody deleteMeal(@RequestBody Map<String, Object> request) {
        try {
            ResBody resBody = new ResBody(ErrorDef.SUCCESS, "删除套餐成功");
            service.deleteMeal((String)request.get("id"));
            return resBody;
        } catch (BusinessException e) {
            return new ResBody(e.getCode(), e.getMessage());
        } catch (Exception e) {
            LOGGER.error("", e);
            return new ResBody(ErrorDef.SYSTEM_ERROR, "删除套餐失败");
        }
    }
}
